%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Frecuency reaction test - See XXXXXXXXXXX paper.
%
% 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% user defined data:
Hz = 1;
trials = 10;
stimulusTime = 0.100

% end user defined data
nonStimulusTime = 1/Hz - stimulusTime


data = NaN(1, trials);

pressed = 0;
t_pressed = 0;
window = 0.6;    

screenNum=0;
res=[640 480];
clrdepth=32;
[wPtr, rect]=Screen('OpenWindow', screenNum, 0, [0 0 res(1) res(2)], clrdepth);

index = 1;
white=WhiteIndex(wPtr);
black=BlackIndex(wPtr);
Screen('FillRect', wPtr, white);

refresh = Screen('GetFlipInterval', wPtr);
vbl = Screen('Flip', wPtr);

for i=1:2*trials
        pressed = 0;
        t_pressed = 0;
        
        if mod(i,2) == 0
            while(pressed == 0 && (vbl + nonStimulusTime + stimulusTime) > GetSecs)
                [pressed, t_pressed] = KbCheck;                
            end;
            
            Screen('FillOval', wPtr, [250 0 0]);
            vbl = Screen('Flip', wPtr, vbl + nonStimulusTime + stimulusTime);
            % flipped to Stimulus screen
            
        else
            while(pressed == 0 && (vbl + stimulusTime) > GetSecs)
                [pressed, t_pressed] = KbCheck;                
            end;
            
            Screen('FillRect', wPtr, white);
            Screen('Flip', wPtr, vbl + stimulusTime);
            % flipped to Blank screen

        end;
        
        if(pressed == 1 && isnan(data(index)))
            data(index) = t_pressed - vbl;
        end;
        if(mod(i,2) == 0)
            index = index+1;
        end;
    
end;



Screen('CloseAll');
ShowCursor;
data
plot(data);


    
